package com.maochang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maochang.mapper.PsUserMapper;
import com.maochang.pojo.PsInspectiontaskDetail;
import com.maochang.pojo.PsSolverDetail;
import com.maochang.pojo.PsUser;
import com.maochang.pojo.util.ChuAnSuoKuAng;
import com.maochang.pojo.util.Result1;
import com.maochang.pojo.util.ZhuRenWuXiaoQueMingXi;
import com.maochang.service.PsSolvetaskDetailService;
import com.maochang.service.PsUserService;
import com.maochang.service.xiaoQuZhiDingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/XiaoQuGuanLi")
public class XiaoQuGuanLi {

    @Resource
    private PsUserService psUserServiceImpl;
    @Resource
    private xiaoQuZhiDingService xiaoQuZhiDingImpl;
    @Resource
    private PsSolvetaskDetailService PsSolvetaskDetailServiceImpl;
    @Resource
    private PsUserMapper psUserMapper;
    /**
     * 消缺任务的指定与 分配跳转
     * @return
     * @throws IOException
     */
    @GetMapping("/XqRenWuZhiDingFenPei")
    public ModelAndView YongHuGuLi() throws IOException {
        return new ModelAndView("XiaoQueRenWuGuanLi/XiaoQueRenWuZhiDingYuFenPei");
    }

    /**
     * 消缺任务回执跳转
     * @return
     * @throws IOException
     */
    @GetMapping("/RenWuHuiZhi")
    public ModelAndView  RenWuHuiZhi() throws IOException {
        return new ModelAndView("XiaoQueRenWuGuanLi/XiaoQuRenWuZhiXingYuHuiZhi/XqRenWuZhiXingHuiZhi");
    }
    /**
     * 消缺查询 跳转
     */
    @GetMapping("/XiAoQuChaXun")
    public ModelAndView  XiAoQuChaXun() throws IOException {
        return new ModelAndView("XiaoQueRenWuGuanLi/XiaoQueChaXun");
    }

    @GetMapping("/xiaoQuYuan")
    public List<PsUser>  xiaoQuYuan() throws IOException {
     List<PsUser> psUsers= psUserServiceImpl.xiaoQuYuan();
       return  psUsers;
    }
    @PostMapping("/ChaChuAnSuoKuang")
    public List<ChuAnSuoKuAng>  chuAnSuoKuAng(){
        System.out.println("aa");
        //自己生成的 集合  list
        List<ChuAnSuoKuAng> cha=new ArrayList<>();
        List<PsUser>  psUsersLsit= psUserServiceImpl.zhenZhengDeXunJiYuan();
        for (PsUser i : psUsersLsit) {
            //自己设置的类
            ChuAnSuoKuAng chuAnSuoKuAng=new ChuAnSuoKuAng();
            chuAnSuoKuAng.setValue(i.getId().toString());
            chuAnSuoKuAng.setTitle(i.getUserName());
            cha.add(chuAnSuoKuAng);
        }
        return  cha;
    }

    @PostMapping("/AddXiaoQuRenWu")
    public Result1 AddXunJiAnRenWu(@RequestBody ZhuRenWuXiaoQueMingXi xi, HttpSession session){
        Result1 result1=new Result1();
        boolean row=xiaoQuZhiDingImpl.addXiaoQu(xi,session);
        if(row){
            //是否取消
            result1.setMsg("添加消缺信息成功");
            //巡检员明细表
        }
        return  result1;
    };
    /**
     * 消缺任务查看按钮
     */
    @GetMapping("/XiaoQuAnNanNiuChKanXiaoQuRenWU")
    public Result1 XiaoQuAnNanNiuChKanXiaoQuRenWU(@RequestParam("id") Integer id,@RequestParam("page") int current, @RequestParam("limit") int size){
        System.out.println("传值陈宫"+id);
        Page<PsInspectiontaskDetail> aa= PsSolvetaskDetailServiceImpl.chaXueXin(id,current,size);
        return  Result1.success1(aa);
    };
    /**
     * 查询巡检子表
     */
    @GetMapping("/chaXunJianYuan")
    public Result1 chaXunJianYuan(@RequestParam("id") Integer id,@RequestParam("page") int current, @RequestParam("limit") int size){
        System.out.println("传值陈宫"+id);
        Page<PsInspectiontaskDetail> aa= PsSolvetaskDetailServiceImpl.chaXueXin(id,current,size);
        return  Result1.success1(aa);
    };
    /**
     * 查巡检员
     */
    @PostMapping("/chaXunJianYuanChanKanGongNeng")
    public List<PsSolverDetail> chaXunJianYuan2(@RequestParam("id") Integer id){
        List<PsSolverDetail> aa= xiaoQuZhiDingImpl.chaXiaoQuRen(id);
        System.out.println(aa.toString());
        return  aa;
    };
    /**
     * 分配消缺任务
     */
     @PostMapping("/xiaoQueRenWuFenPei")
     public boolean xiaoQueRenWuFenPei(@RequestParam("id") Integer id,@RequestBody List<ChuAnSuoKuAng> chuAnSuoKuAng,HttpSession session){
         boolean aa= xiaoQuZhiDingImpl.FenPeiXiAoQueRenWu(id,chuAnSuoKuAng,session);
         System.out.println(aa);
         return  aa;
     };
    /**
     * 查询 负责人 某个负责人
     */
    @PostMapping("/chaMouGeFuZeRen")
    public PsUser chaMouGeFuZeRen(String id){
        System.out.println(id+"code 值");
        QueryWrapper<PsUser> wrapper= Wrappers.query();
        wrapper.eq("userCode",id);
        PsUser aa= psUserMapper.selectOne(wrapper);
        System.out.println(aa);
        return  aa;
    };
}
